package com.wmcom.wm.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.wmcom.wm.common.R;
import com.wmcom.wm.dto.SetmealDto;
import com.wmcom.wm.entity.Category;
import com.wmcom.wm.entity.Setmeal;
import com.wmcom.wm.entity.SetmealDish;
import com.wmcom.wm.service.CategoryService;
import com.wmcom.wm.service.SetmealDishService;
import com.wmcom.wm.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 套餐管理
 *
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
     @Resource
     private SetmealService setmealService;
     @Resource
     private SetmealDishService setmealDishService;
     @Resource
     private CategoryService categoryService;
    /**
     *
     * 显示所以套餐
     */
 @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name) {
            Page<Setmeal> pa = new Page<>(page, pageSize);
            Page<SetmealDto> pa1=new Page<>();
             LambdaQueryWrapper<Setmeal> wrap=new LambdaQueryWrapper<>();
            if(name!=null){
                  wrap.like(Setmeal::getName,name);
            }
            wrap.orderByDesc(Setmeal::getPrice);

            setmealService.page(pa,wrap);
            //对象拷贝
           BeanUtils.copyProperties(pa,pa1,"records");
           List<Setmeal> records = pa.getRecords();
            List<SetmealDto> pages=null;
                   pages= records.stream().map((item)->{
                 SetmealDto setmealDto=new SetmealDto();
                 BeanUtils.copyProperties(item,setmealDto);
                Long categoryId = item.getCategoryId();
                Category category = categoryService.getById(categoryId);
                if (category != null) {
                    String name1 = category.getName();
                    setmealDto.setCategoryName(name1);
                }
                return setmealDto;
            }).collect(Collectors.toList());
            pa1.setRecords(pages);
             return R.success(pa1);
    }
    /**
     * 根据id查询套餐信息
     */
    @GetMapping("/{id}")
    public R<SetmealDto> secma(@PathVariable long id){
        Setmeal setmeal =  setmealService.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);
        LambdaQueryWrapper<SetmealDish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list=setmealDishService.list(wrapper);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }
    /**
     * 新增套餐
     */
     @PostMapping
     @Transactional()
     public R<String>insert(@RequestBody SetmealDto setmealDto){
         setmealService.save(setmealDto);
         List<SetmealDish> setmealfiles = setmealDto.getSetmealDishes();
         setmealfiles.forEach(setmealDish ->
                 setmealDish.setSetmealId(setmealDto.getId()));
          setmealDishService.saveBatch(setmealfiles);
         return R.success("添加成功！");
}
    /**
     * 修改套餐
     */
    @PutMapping
    public R<String> updateSetmealfiles(@RequestBody SetmealDto setmealDto){
         setmealService.updateById(setmealDto);
        List<SetmealDish> setmealdishes = setmealDto.getSetmealDishes();
        setmealdishes.forEach(setmealDish ->
               setmealDishService.updateById(setmealDish)
                );
        return R.success("修改成功！");
    }
    /**
     * 套餐批量删除
     */
    @DeleteMapping
    public R<String> delete(long[] ids){
         for(long id:ids){
              setmealService.removeById(id);
         }
         return R.success("修改成功！");
    }
    /**
     * 批量停售
     */
    @PostMapping("/status/{status}")
    public R<String>status(long []ids,@PathVariable int status ){
            for (long id : ids) {
                LambdaQueryWrapper<Setmeal>wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Setmeal::getId, id);
                Setmeal setmeal = setmealService.getOne(wrapper);
                setmeal.setStatus(status);
                setmealService.updateById(setmeal);
            }
            return R.success("批量停售成功1");
    }
    @GetMapping("/list")
    public R<List<Setmeal>>list(Setmeal setmeal){
           LambdaQueryWrapper<Setmeal>wrapper = new LambdaQueryWrapper<>();
           wrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
           wrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
           wrapper.orderByDesc(Setmeal::getUpdateTime);
        List<Setmeal> list = setmealService.list(wrapper);
        return R.success(list);
    }

}
